﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.virals {
    import Engine.Managers.*;
    import flash.utils.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Display.*;
    import Modules.stats.experiments.*;
    import Engine.*;
    import com.zynga.skelly.util.*;

    public class ViralManager {

        private static const PERFORM_IMMEDIATELY:Boolean = true;
        public static const STREAM_PUBLISH_AUTO:int = 0;
        public static const STREAM_PUBLISH_DEFAULT:int = 1;
        public static const STREAM_PUBLISH_FAIL:int = 2;

        private static var m_FreezeTimerStart:Number = -1;
        private static var m_ThawTimerStart:Number = -1;
        private static var m_viralFreezeStart:Number = -1;
        private static var m_viralFreezeTimeout:Number = -1;
        private static var m_customFreezeText:String = null;

        private var m_activeVirals:Dictionary;

        public function ViralManager(){
            this.m_activeVirals = new Dictionary();
            m_viralFreezeStart = Global.gameSettings.getNumber("viralFreezeStart", 500);
            m_viralFreezeTimeout = Global.gameSettings.getNumber("viralFreezeTimeout", 5000);
        }
        private static function resetFreezeTimer(_arg1:String=null):void{
            m_FreezeTimerStart = GlobalEngine.getTimer();
            m_customFreezeText = _arg1;
        }
        private static function resetThawTimer():void{
            m_ThawTimerStart = GlobalEngine.getTimer();
        }
        public static function doViralManagerThawCheck():void{
            var _local3:Number;
            var _local4:Number;
            if ((((m_ThawTimerStart == -1)) && ((m_FreezeTimerStart == -1)))){
                return;
            };
            var _local1:Boolean;
            if (m_FreezeTimerStart != -1){
                _local3 = (GlobalEngine.getTimer() - m_FreezeTimerStart);
                if (_local3 > m_viralFreezeStart){
                    _local1 = true;
                };
            };
            if (_local1){
                UI.freezeScreen(true, false, m_customFreezeText);
                m_FreezeTimerStart = -1;
                resetThawTimer();
                return;
            };
            var _local2:Boolean;
            if (m_ThawTimerStart != -1){
                _local4 = (GlobalEngine.getTimer() - m_ThawTimerStart);
                if (_local4 > m_viralFreezeTimeout){
                    _local2 = true;
                };
            };
            if (_local2 == true){
                doUIThaw();
            };
        }
        public static function doUIThaw():void{
            if (m_ThawTimerStart == -1){
                return;
            };
            UI.thawScreen();
            m_customFreezeText = null;
            m_FreezeTimerStart = -1;
            m_ThawTimerStart = -1;
        }

        public function loadActiveVirals(_arg1:Object):void{
            var _local2:String;
            var _local3:Viral;
            for (_local2 in _arg1) {
                _local3 = new Viral(_local2, uint(_arg1[_local2].timesPosted), Number(_arg1[_local2].originTime), _arg1[_local2].recipients);
                this.m_activeVirals[_local2] = _local3;
            };
        }
        public function getActiveViralByType(_arg1:String):Viral{
            return (this.m_activeVirals[_arg1]);
        }
        public function canPost(_arg1:String, _arg2:String=null):Boolean{
            var _local3:Viral = this.m_activeVirals[_arg1];
            if (((((!((_local3 == null))) && (_local3.hasHitPostLimit(_arg2)))) && (!(_local3.hasReset())))){
                return (false);
            };
            return (true);
        }
        public function postViral(_arg1:String, _arg2:String=null):void{
            this.purgeResetVirals();
            var _local3:Viral;
            if (this.m_activeVirals.hasOwnProperty(_arg1)){
                _local3 = this.m_activeVirals[_arg1];
            };
            if (_local3 == null){
                if (_arg2){
                    this.m_activeVirals[_arg1] = new Viral(_arg1, 0, (GlobalEngine.serverTime / 1000));
                    this.m_activeVirals[_arg1].addPost(_arg2);
                } else {
                    this.m_activeVirals[_arg1] = new Viral(_arg1, 1, (GlobalEngine.serverTime / 1000));
                };
            } else {
                if (_local3.hasHitPostLimit(_arg2) == false){
                    _local3.addPost(_arg2);
                };
            };
        }
        protected function purgeResetVirals():void{
            var _local1:Viral;
            for each (_local1 in this.m_activeVirals) {
                if (_local1.hasReset()){
                    this.m_activeVirals[_local1.getType()] = null;
                    delete this.m_activeVirals[_local1.getType()];
                };
            };
        }
        protected function sendStreamPublish(_arg1:String, _arg2:Object, _arg3:String=null, _arg4:Object=null, _arg5:String=null, _arg6:Function=null):Boolean{
            if (this.canPost(_arg1)){
                if (((!((_arg3 == null))) && (!((_arg3 == Global.player.uid))))){
                    resetFreezeTimer();
                } else {
                    if (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_FUSION_API_ENABLE) == ExperimentDefinitions.FUSION_API_ENABLE){
                        resetFreezeTimer();
                    };
                };
                GameTransactionManager.addTransaction(new TCityStreamPublish(_arg1, _arg2, _arg3, _arg4, null, _arg5, _arg6), PERFORM_IMMEDIATELY, true);
                this.postViral(_arg1, _arg3);
                return (true);
            };
            return (false);
        }
        protected function sendAutoStreamPublish(_arg1:String, _arg2:Object, _arg3:String=null, _arg4:Object=null, _arg5:String=null, _arg6:Boolean=true, _arg7:Function=null):Boolean{
            var _local8:Boolean;
            if (this.canPost(_arg1)){
                resetFreezeTimer("FrameLoading_Viral");
                if (!GlobalEngine.socialNetwork.userHasStreamPermissions()){
                    GlobalEngine.socialNetwork.showStreamPermissions(Curry.curry(this.onStreamPermissionsClosed, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7));
                    _local8 = true;
                } else {
                    _local8 = this.sendAutoStreamPublishHelper(_arg1, _arg2, _arg3, _arg4, _arg5, _arg7);
                };
            };
            return (_local8);
        }
        protected function onStreamPermissionsClosed(_arg1:String, _arg2:Object, _arg3:String=null, _arg4:Object=null, _arg5:String=null, _arg6:Boolean=true, _arg7:Function=null):void{
            var _local8:Boolean;
            if (GlobalEngine.socialNetwork.userHasStreamPermissions()){
                _local8 = this.sendAutoStreamPublishHelper(_arg1, _arg2, _arg3, _arg4, _arg5, _arg7);
                if (!_local8){
                    _arg7(STREAM_PUBLISH_FAIL);
                };
            } else {
                if (_arg6){
                    this.sendStreamPublish(_arg1, _arg2, _arg3, _arg4, _arg5, _arg7);
                };
            };
        }
        protected function sendAutoStreamPublishHelper(_arg1:String, _arg2:Object, _arg3:String=null, _arg4:Object=null, _arg5:String=null, _arg6:Function=null):Boolean{
            if (this.canPost(_arg1)){
                resetFreezeTimer("FrameLoading_Viral");
                GameTransactionManager.addTransaction(new TCityStreamPublish(_arg1, _arg2, _arg3, _arg4, null, _arg5, _arg6, true), PERFORM_IMMEDIATELY);
                this.postViral(_arg1, _arg3);
                return (true);
            };
            return (false);
        }
        public function sendLevelUpFeed(_arg1:Player, _arg2:Number):Boolean{
            var _local3:String = _arg1.levelUpNickName(_arg2);
            var _local4:Array = new Array();
            if (_arg2 < 30){
                _local4.push(ViralType.LEVEL_UP_01);
                _local4.push(ViralType.LEVEL_UP_02);
                _local4.push(ViralType.LEVEL_UP_03);
            } else {
                if (_arg2 < 60){
                    _local4.push(ViralType.LEVEL_UP_04);
                    _local4.push(ViralType.LEVEL_UP_05);
                    _local4.push(ViralType.LEVEL_UP_06);
                } else {
                    if (_arg2 < 90){
                        _local4.push(ViralType.LEVEL_UP_07);
                        _local4.push(ViralType.LEVEL_UP_08);
                        _local4.push(ViralType.LEVEL_UP_09);
                    } else {
                        _local4.push(ViralType.LEVEL_UP_10);
                    };
                };
            };
            var _local5:int = Math.round(Utilities.randBetween(0, (_local4.length - 1)));
            var _local6:String = _local4[_local5];
            return (this.sendStreamPublish(_local6, {
                level:_arg2,
                nickName:_local3
            }, _arg1.uid));
        }
        public function sendQuestCompleteFeed(_arg1:Player, _arg2:String, _arg3:String):Boolean{
            return (this.sendStreamPublish(ViralType.QUEST_COMPLETE, {
                quest:_arg2,
                questName:_arg3,
                cityname:Global.player.cityName
            }, _arg1.uid));
        }
        public function sendCollectionTradeInFeed(_arg1:Player, _arg2:String):Boolean{
            return (this.sendStreamPublish(ViralType.COLLECTION_TRADE_IN, {collection:_arg2}, _arg1.uid));
        }
        public function sendEnergyFeed(_arg1:Player):Boolean{
            return (this.sendStreamPublish(ViralType.ENERGY_FEED, {}, _arg1.uid));
        }
        public function sendNameYourCityFeed(_arg1:Player, _arg2:String):Boolean{
            return (this.sendStreamPublish(ViralType.NAME_YOUR_CITY, {cityname:_arg2}, _arg1.uid));
        }
        public function sendMunicipalMaterialFeed(_arg1:Player, _arg2:String, _arg3:Item):Boolean{
            return (this.sendCustomMaterialFeed("municipal_", _arg1, _arg2, _arg3));
        }
        public function sendCustomMaterialFeed(_arg1:String, _arg2:Player, _arg3:String, _arg4:Item):Boolean{
            var _local5:Boolean = this.sendStreamPublish((_arg1 + _arg4.name), {
                building:_arg3,
                material:_arg4.name,
                materialName:_arg4.localizedName
            }, _arg2.uid);
            return (_local5);
        }
        public function sendWishlistRequest(_arg1:Player):Boolean{
            return (this.sendStreamPublish(ViralType.WISHLIST_REQUEST, {}, _arg1.uid));
        }
        public function sendNeighborVisitFeed_Default(_arg1:Player, _arg2:String, _arg3:String):Boolean{
            return (this.sendStreamPublish(ViralType.NEIGHBOR_VISIT_DEFAULT, {toCity:_arg3}, _arg2));
        }
        public function sendNeighborVisitFeed_Bus(_arg1:Player, _arg2:String, _arg3:String, _arg4:String):Boolean{
            return (this.sendStreamPublish(ViralType.NEIGHBOR_VISIT_BUS, {
                fromCity:_arg2,
                toCity:_arg4
            }, _arg3));
        }
        public function sendNeighborVisitFeed_Crop(_arg1:Player, _arg2:String):Boolean{
            return (this.sendStreamPublish(ViralType.NEIGHBOR_VISIT_CROPS, {}, _arg2));
        }
        public function sendNeighborVisitFeed_Ship(_arg1:Player, _arg2:String):Boolean{
            return (this.sendStreamPublish(ViralType.NEIGHBOR_VISIT_SHIPS, {}, _arg2));
        }
        public function sendNeighborVisitFeed_Build(_arg1:Player, _arg2:String):Boolean{
            return (this.sendStreamPublish(ViralType.NEIGHBOR_VISIT_BUILD, {}, _arg2));
        }
        public function sendReputationUpFeed(_arg1:Player, _arg2:Number):Boolean{
            return (this.sendStreamPublish(ViralType.REPUTATION_LEVEL_UP, {
                level:_arg2,
                cityname:Global.player.cityName
            }, _arg1.uid));
        }
        public function sendQuestFeed(_arg1:Player, _arg2:String):Boolean{
            return (this.sendStreamPublish(_arg2, {cityname:Global.player.cityName}, _arg1.uid));
        }
        public function sendFranchiseFeed_EmptyLot(_arg1:Player, _arg2:String):Boolean{
            return (this.sendStreamPublish(ViralType.FRANCHISE_EMPTY_LOT, {cityname:_arg2}, _arg1.uid));
        }
        public function sendFranchiseFeed_BuildingRequest(_arg1:String, _arg2:String):Boolean{
            return (this.sendStreamPublish(ViralType.FRANCHISE_BUILDING_REQUEST, {franchiseName:_arg1}, _arg2));
        }
        public function sendFranchiseFeed_AcceptedBuilding(_arg1:String, _arg2:String, _arg3:String):Boolean{
            return (this.sendStreamPublish(ViralType.FRANCHISE_ACCEPTED_BUILDING, {
                cityname:_arg1,
                franchiseName:_arg2
            }, _arg3));
        }
        public function sendFranchiseFeed_DeclinedBuilding(_arg1:String, _arg2:String, _arg3:String):Boolean{
            return (this.sendStreamPublish(ViralType.FRANCHISE_DECLINED_BUILDING, {
                cityname:_arg1,
                franchiseName:_arg2
            }, _arg3));
        }
        public function sendFranchiseFeed_RemovedBuilding(_arg1:String, _arg2:String, _arg3:String):Boolean{
            return (this.sendStreamPublish(ViralType.FRANCHISE_REMOVED_BUILDING, {
                cityname:_arg1,
                franchiseName:_arg2
            }, _arg3));
        }
        public function sendFranchiseFeed_StarRatingIncreased(_arg1:Player, _arg2:String, _arg3:String, _arg4:Number):Boolean{
            return (this.sendStreamPublish(ViralType.FRANCHISE_STAR_RATING_INCREASED, {
                franchiseName:_arg3,
                friendCity:_arg2,
                starsLevel:_arg4
            }, _arg1.uid));
        }
        public function sendFranchiseFeed_GrowHQ(_arg1:Player, _arg2:String):Boolean{
            return (this.sendStreamPublish(ViralType.FRANCHISE_GROW_HQ, {franchiseName:_arg2}, _arg1.uid));
        }
        public function sendFranchiseFeed_ReminderAcceptBonus(_arg1:String, _arg2:String, _arg3:String, _arg4:String):Boolean{
            return (this.sendStreamPublish(ViralType.FRANCHISE_BONUS_REMIND_ACCEPT, {
                cityname:_arg1,
                franchiseName:_arg2,
                friendCity:_arg4
            }, _arg3));
        }
        public function sendTrainTradeViral_Buy(_arg1:Player, _arg2:String, _arg3:String):Boolean{
            return (this.sendStreamPublish(ViralType.TRAIN_TRADE_BUY, {commodityName:_arg3}, _arg2));
        }
        public function sendTrainTradeViral_Sold(_arg1:Player, _arg2:String, _arg3:String):Boolean{
            return (this.sendStreamPublish(ViralType.TRAIN_TRADE_SOLD, {commodityName:_arg3}, _arg2));
        }
        public function sendTrainTradeViral_Sell(_arg1:Player, _arg2:String, _arg3:String):Boolean{
            return (this.sendStreamPublish(ViralType.TRAIN_TRADE_SELL, {commodityName:_arg3}, _arg2));
        }
        public function sendNewsViralFeed(_arg1:Player, _arg2:String, _arg3:String, _arg4:String):Boolean{
            return (this.sendStreamPublish(ViralType.NEWS_VIRAL, {
                newsTitle:_arg2,
                newsSubhead:_arg3,
                newsMessage:_arg4
            }, _arg1.uid));
        }
        public function sendUGCViralFeed(_arg1:Player, _arg2:String, _arg3:String, _arg4:String):Boolean{
            return (this.sendStreamPublish(ViralType.UGC_VIRAL, {
                franchiseType:_arg3,
                franchiseName:_arg4,
                cityname:_arg2
            }, _arg1.uid));
        }
        public function sendBecomeNeighborFeed():Boolean{
            return (this.sendStreamPublish(ViralType.BECOME_NEIGHBOR, {}, Global.player.uid));
        }
        public function sendDailyBonusFeed():Boolean{
            return (this.sendStreamPublish(ViralType.DAILY_BONUS, {}, Global.player.uid));
        }
        public function sendPermitsRequest(_arg1:Player, _arg2:String):Boolean{
            return (this.sendStreamPublish(ViralType.PERMITS_REQUEST, {cityname:_arg2}, _arg1.uid));
        }
        public function sendHoliday2010PresentRequest(_arg1:Player):Boolean{
            return (this.sendStreamPublish(ViralType.HOLIDAY2010_TREE_GIFT_REQUEST, {}, _arg1.uid));
        }
        public function sendTrainFeed(_arg1:String, _arg2:String, _arg3:String):Boolean{
            var _local4:Item = Global.gameSettings.getItemByName(_arg1);
            if (_local4){
                return (this.sendStreamPublish(_local4.feed, {cityname:_arg2}, Global.player.uid, {bucket:_arg3}));
            };
            return (false);
        }
        public function sendZooFeed(_arg1:String, _arg2:String):Boolean{
            var _local3:Item = Global.gameSettings.getItemByName(_arg1);
            if (_local3){
                return (this.sendStreamPublish(_local3.feed, {cityname:_arg2}, Global.player.uid, {enclosure:_arg1}));
            };
            return (false);
        }
        public function sendRollCallCollectReminder(_arg1:String, _arg2:MapResource):Boolean{
            return (this.sendAutoStreamPublish(ViralType.ROLL_CALL_COLLECTION_REMINDER, {
                title:_arg2.getCrewPositionName(_arg1),
                buildingName:_arg2.getItemFriendlyName()
            }, _arg1, {
                action:"collectionReminder",
                bid:_arg2.getId()
            }));
        }
        public function sendRollCallCheckInFeed(_arg1:String, _arg2:Object, _arg3:String, _arg4:Function=null):Boolean{
            var _local5:String = Global.player.getFriendFirstName(_arg1);
            if (!_local5){
                _local5 = " ";
            };
            return (this.sendAutoStreamPublish(ViralType.ROLL_CALL_CHECKIN, {
                cityname:Global.player.cityName,
                recipient:_local5
            }, _arg1, _arg2, _arg3, true, _arg4));
        }
        public function sendRollCallNotifyCheckInFeed(_arg1:String, _arg2:MapResource):Boolean{
            return (this.sendStreamPublish(ViralType.ROLL_CALL_NOTIFY_CHECKIN, {
                owner:Global.player.getFriendFirstName(_arg1),
                cityname:Global.player.getFriendCityName(_arg1),
                buildingName:_arg2.getItemFriendlyName()
            }, _arg1, {
                action:"notifyCheckIn",
                bid:_arg2.getId(),
                ownerId:_arg1
            }));
        }
        public function vdaySendCardThankYou(_arg1:String):Boolean{
            return (this.sendStreamPublish(ViralType.VDAY2011_CARD_THANK_YOU, {}, _arg1));
        }
        public function vdayBragAboutAdmirers():Boolean{
            return (this.sendStreamPublish(ViralType.VDAY2011_ADMIRER_BRAG, {}, Global.player.uid));
        }
        public function vdayBragAboutAchievements():Boolean{
            return (this.sendStreamPublish(ViralType.VDAY2011_ACHIEVEMENTS_BRAG, {}, Global.player.uid));
        }
        public function visitorUISendNameFeed():Boolean{
            return (this.sendStreamPublish(ViralType.VISITORUI, {cityname:Global.player.cityName}, Global.player.uid));
        }
        public function sendGetDonut():Boolean{
            return (this.sendStreamPublish(ViralType.COPS_N_BANDITS_GET_DONUT, {cityname:Global.player.cityName}, Global.player.uid));
        }
        public function sendGetDonutForPeddlerCenter():Boolean{
            return (this.sendStreamPublish(ViralType.PEDDLER_CENTER_GET_DONUT, {cityname:Global.player.cityName}, Global.player.uid));
        }
        public function sendBanditCapture(_arg1:int, _arg2:String):Boolean{
            var _local3:String = ((ViralType.COPS_N_BANDITS_CAPTURE + "_") + _arg1);
            return (this.sendStreamPublish(_local3, {
                cityname:Global.player.cityName,
                cop:_arg2
            }, Global.player.uid));
        }
        protected function sendItemRequest(_arg1:String, _arg2:String=null):void{
            var _local3:String = ("request.php?itemName=" + _arg1);
            if (_arg2){
                _local3 = (_local3 + ("&requestType=" + _arg2));
            };
            FrameManager.showTray(_local3);
        }
        public function sendGift(_arg1:String):void{
        }
        public function sendEnergyRequest():Boolean{
            if (this.canPost(ViralType.ENERGY_REQUEST)){
                this.sendItemRequest("energy_3", "energy_request");
                this.postViral(ViralType.ENERGY_REQUEST);
                return (true);
            };
            return (false);
        }
        public function sendMunicipalMaterialRequest(_arg1:Player, _arg2:String, _arg3:Item):Boolean{
            return (this.sendCustomMaterialRequest("municipal_", _arg1, _arg2, _arg3));
        }
        public function sendCustomMaterialRequest(_arg1:String, _arg2:Player, _arg3:String, _arg4:Item):Boolean{
            var _local5:String = (_arg1 + _arg4.name);
            if (this.canPost(_local5)){
                this.sendItemRequest(_arg4.name, _local5);
                this.postViral(_local5);
                return (true);
            };
            return (false);
        }
        public function canSendRollCallCheckInFBMessage(_arg1:String):Boolean{
            var _local2:int = (Global.gameSettings.getInt("fbRollCallMessageCooldown") * 3600);
            var _local3:int = Global.player.getLastActivationTime(("rollCall" + _arg1));
            var _local4:int = uint((GlobalEngine.getTimer() / 1000));
            return ((_local4 > (_local3 + _local2)));
        }
        public function sendRollCallCheckFBMessage(_arg1:String, _arg2:int, _arg3:String=null):void{
            var _local4:int = uint((GlobalEngine.getTimer() / 1000));
            Global.player.setLastActivationTime(("rollCall" + _arg1), _local4);
            GameTransactionManager.addTransaction(new TSendRollCallFacebookMessage(_arg1, _arg2, _arg3), true);
            var _local5:MechanicMapResource = (Global.world.getObjectById(_arg2) as MechanicMapResource);
            if (_local5){
                StatsManager.social("fb_chat", _arg1, "roll_call", _local5.getItemName(), _local5.getCrewPositionName(_arg1));
            };
        }

    }
}//package Classes.virals 
